﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Statistics
{
    internal class Func
    {
        private Dictionary<string, int> wordList = new Dictionary<string, int>();

        public static int CountWords(string text)
        {
            string[] words = text.Split(new[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            return words.Length;
        }

        public static string Remove(string text)
        {
            string pattern = @"^\s*(//.*)?$"; // Matches empty lines and lines with single-line comments
            string[] lines = text.Split('\n');
            List<string> nonEmptyLines = new List<string>();

            foreach (string line in lines)
            {
                if (!Regex.IsMatch(line, pattern))
                {
                    nonEmptyLines.Add(line);
                }
            }

            return string.Join("\n", nonEmptyLines);
        }


        public static Dictionary<string, int> CountWordOccurrences(string text)
        {
            string[] words = text.Split(new[] { ' ', '\t', '\r', '\n', '.', ',', ';', ':', '(', ')', '{', '}', '[', ']', '<', '>', '=', '+', '-', '*', '/', '%', '!', '?', '&', '|', '^', '@', '#', '$', '_', '"' }, StringSplitOptions.RemoveEmptyEntries);
            Dictionary<string, int> wordCounts = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);

            foreach (string word in words)
            {
                if (wordCounts.ContainsKey(word))
                {
                    wordCounts[word]++;
                }
                else
                {
                    wordCounts[word] = 1;
                }
            }

            return wordCounts;
        }
    }
}
